* Elderly outcomes in critical care DO file *

cd "/Users/benmorton/Documents/Ben's documents/Research/Projects/Elderly outcomes"

clear
import excel "Reference Survival.xls", sheet("Sheet1") firstrow
sort (_year sex _age)
drop if sex ==.
save RS, replace

clear 

import excel "Elderly for analysis 2019 GDV3", sheet("Sheet1") firstrow
set more off
save AnalysisRS, replace


recode ITU_death_days (min/1825.1=1), gen(event2)
replace event2 = 0 if event2 ==.
replace event2 = 0 if event2 >1825.1
recode Days_alive (1825.1/max=1825), gen(TTE)
drop if TTE<0
stset TTE, failure (event2==1)

gen year = real(substr(Episode_No,1,4))
ren Gender sex
ren Age age
ren year yydx
gen id = _n

label define diag 1 "medical" 2 "surgical"
label values Med_Surg_Diag diag

label define gender 1 "male" 2 "female"
label values sex gender

recode Lowest_pH (min/7.05=1) (7.051/7.15=2) (7.151/7.25=3) (7.251/7.35=4) (7.351/7.45=5) (7.451/7.55=6) (7.55/max=7), gen(pHcategory)
label define pH 1 "<7.05" 2 "7.05-7.15" 3 "7.15-7.25" 4 "7.25-7.35" 5 "7.35-7.45" 6 "7.45-7.55" 7 ">7.55"
label values pHcategory pH
tab pHcategory

recode Lactate (min/2=1) (2.01/4=2) (4.01/6=3) (6.01/8=4) (8.01/max=5), gen(Lactatecategory)
label define lac 1 "0-2" 2"2-4" 3 "4-6" 4 "6-8" 5 ">8"
label values Lactatecategory lac
tab Lactatecategory

recode Lowest_SBP (min/70=1) (70.01/80=2) (80.01/90=3) (90.01/100=4) (100.01/110=5) (110.01/max=6), gen(SBPcat)
label define sbp 1 "<70" 2 "<80" 3 "<90" 4 "<100" 5 "<110" 6 ">110"
label values SBPcat sbp
tab SBPcat

gen GCSbin =.
recode GCSbin .=0 if Lowest_GCS==8 | Lowest_GCS<8
recode GCSbin .=1 if Lowest_GCS>8
label define gcs 1 "GCS 9 or more" 0 "GCS 8 or less"
label values GCSbin gcs
tab GCSbin


drop if id>827

stset TTE, failure (event2==1) id(id) scale (365.25)

strs using RS, br(0(0.083333)5) mergeby(_year sex _age) save(replace)
			 
strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(Med_Surg_Diag) save(replace)

strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(pHcategory) save(replace)

strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(Lactatecategory) save(replace)

strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(SBPcat) save(replace)

strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(sex) save(replace)


strs using RS, br(0(0.083333)5) mergeby(_year sex _age) by(Med_Surg_Diag pHcategory) save(replace)



